package com.src.gui;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.TimeZone;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;


public class PickDate extends JDialog implements ActionListener {

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;

	private JButton forward_mo = null;
	private JButton backward_mo = null;
	private JLabel cmonth = null;
	private JLabel cyear = null;
	
	private Calendar cal;
	private JButton daysofmonth[];
	
	private String pickDate;

	private JPanel parent;
	private boolean start;
	
	final private String[]  months = {"January","February","March","April","May",
			"June","July","August","September","October","November","December"};
	
	int CMONTH;
	int CYEAR;
	int CDAY;
	
	/**
	 * This is the default constructor
	 */
	public PickDate(JPanel event, boolean start) {
		super();
		this.parent = (EventView)event;
		this.start = start;
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		cal = new GregorianCalendar(TimeZone.getTimeZone("EDT"));
		cal = Calendar.getInstance();
		
		CMONTH = cal.get(Calendar.MONTH);
		CYEAR = cal.get(Calendar.YEAR);
		CDAY = cal.get(Calendar.DAY_OF_MONTH);
		
		daysofmonth= new JButton[42];
		
		
		this.setSize(370, 270);
		this.setContentPane(getJContentPane());
		this.setTitle("Pick Time");
		this.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
		this.setLocationRelativeTo(parent);
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			
			//construct the control bar
			// << Month >>
			JPanel controlPane = new JPanel(new GridLayout(1,3,10,10));
			backward_mo = new JButton("<<");		
			cmonth = new JLabel(months[CMONTH],JLabel.TRAILING);
			cyear = new JLabel(Integer.toString(CYEAR),JLabel.LEADING);
			forward_mo = new JButton(">>");
			controlPane.add(backward_mo);
			controlPane.add(cmonth);
			controlPane.add(cyear);
			controlPane.add(forward_mo);
			
			backward_mo.addActionListener(new java.awt.event.ActionListener(){
				public void actionPerformed(java.awt.event.ActionEvent e)
				{
					CMONTH--;
					if(CMONTH<0)
					{
						CMONTH = 11 ; CYEAR--;
					}
					do_daysofmonth(CMONTH,CYEAR,daysofmonth);
					
					cmonth.setText(months[CMONTH]);
					cyear.setText(String.valueOf(CYEAR));
				}
			});
			
			forward_mo.addActionListener(new java.awt.event.ActionListener(){
				public void actionPerformed(java.awt.event.ActionEvent e)
				{
					CMONTH++;
					if(CMONTH>11)
					{
						CMONTH = 0 ; CYEAR++;
					}
					do_daysofmonth(CMONTH,CYEAR,daysofmonth);
					
					cmonth.setText(months[CMONTH]);
					cyear.setText(String.valueOf(CYEAR));
				}
			});
			
			jContentPane.add(controlPane,BorderLayout.NORTH);
			
			JPanel monthPane = new JPanel(new GridLayout(7,6,2,5));
			
			monthPane.add(new JLabel("Sun",JLabel.TRAILING));
			monthPane.add(new JLabel("Mon",JLabel.TRAILING));
			monthPane.add(new JLabel("Tue",JLabel.TRAILING));
			monthPane.add(new JLabel("Wed",JLabel.TRAILING));
			monthPane.add(new JLabel("Thu",JLabel.TRAILING));
			monthPane.add(new JLabel("Fri",JLabel.TRAILING));
			monthPane.add(new JLabel("Sat",JLabel.TRAILING));
			
			for(int i=0; i<42; i++)
			{
				daysofmonth[i] = new JButton("");
				daysofmonth[i].addActionListener(this);
				daysofmonth[i].setSize(30, 30);
				monthPane.add(daysofmonth[i]);;
			}
			
			do_daysofmonth(CMONTH,CYEAR,daysofmonth);
			
			jContentPane.add(monthPane,BorderLayout.CENTER);
		}
		return jContentPane;
	}

	public void do_daysofmonth(int mo, int yr, JButton todo[])
	{
		Calendar setcal = new GregorianCalendar();
		setcal.set(yr,mo,1);
		
		int startday = setcal.get(Calendar.DAY_OF_WEEK)-1;
		int stopday = setcal.getActualMaximum(Calendar.DAY_OF_MONTH);
		
		for (int i = 0;i<42;i++)
		{
			todo[i].setEnabled(false);
		}
		
		for(int i=0; i<startday;i++)
		{
			todo[i].setText("");	
		}
		for (int i = startday; i< startday+stopday;i++)
		{
			todo[i].setText(String.valueOf(i - startday +1));
			todo[i].setEnabled(true);			
		}
		for (int i=startday+stopday;i<42;i++)
		{
			todo[i].setText("");
		}
		
	}
	
	public String getPickedDate()
	{
		return pickDate;
	}
	
	@Override
	public void actionPerformed(ActionEvent action) {
		
		try
		{
			int day = Integer.valueOf(action.getActionCommand());
			int imonth = CMONTH+1;
			String month = (imonth>=10)? String.valueOf(imonth):("0"+String.valueOf(imonth));
			String sday = (day>=10)?String.valueOf(day):("0"+String.valueOf(day));
			pickDate = String.valueOf(CYEAR)+month+sday;
			
			if(start)
				((EventView) parent).setEventStartDate(pickDate);
			else
				((EventView) parent).setEventEndDate(pickDate);
			
			dispose();
		}
		catch(Exception error)
		{
			error.printStackTrace();
		}

		
		
	}

	
}
